/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package music.emotion.engine;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import music.emotion.Asset;
import music.emotion.engine.facts.Facts;
import music.emotion.engine.rules.Rule;
import music.emotion.songs.Song;

/**
 *
 * @author Morian
 */
public class Engine {

    private boolean finish = false;
    int countx = 0;
    public ArrayList<Rule> rules;
    String str1;
    String str2;
    String str3;

    public Engine() {
        Asset.sampleSongs = new ArrayList<Song>();
        rules = new ArrayList<Rule>();
        try {
            rules = readRules();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(""+rules.size());
        while (!finish) {
//            System.out.println("" + (++countx));
            finish = true;
            for (Rule rule : rules) {
                if (!rule.removed) {
                    if (rule.match(Asset.input)) {
                        str1 = null;
                        str2 = null;
                        str3 = null;
                        finish = false;
                        for (Facts fact : rule.KL) {
//                            System.out.println("Add");
                            Asset.input.add(fact);
                            if (fact.group == 3) {
                                str1 = fact.getName();
                            } else if (fact.group == 4) {
                                str2 = fact.getName();
                            } else if (fact.group == 5) {
                                str3 = fact.getName();
                            }
                        }
                        if(str1 != null && str2 != null && str3 != null){
                            Asset.sampleSongs.add(new Song(str1, str2, str3));
                        }
                        rule.removed = true;
                        if (rules.isEmpty()) {
                            break;
                        }
                    }
                }
            }
        }
//        for (int i = 0; i < Asset.input.size(); i++) {
//            Facts f = Asset.input.get(i);
//            System.out.println("" + " " + f.name + " " + f.content);
//        }
        for (int i = 0; i < Asset.sampleSongs.size(); i++) {
            Song s = Asset.sampleSongs.get(i);
            System.out.println(s.toString());
        }
    }

    public ArrayList<Rule> readRules() throws FileNotFoundException, IOException, ClassNotFoundException {
        ArrayList<Rule> list = new ArrayList();
        //Create new FileInputStream object to read file
        FileInputStream fis = new FileInputStream("src\\music\\emotion\\engine\\file\\Rules");
        //Create new ObjectInputStream object to read object from file
        ObjectInputStream obj = new ObjectInputStream(fis);
        try {
            while (fis.available() != -1) {
                //Read object from file
                Rule acc = (Rule) obj.readObject();
                list.add(acc);
            }
        } catch (EOFException ex) {
            //ex.printStackTrace();
        }
        return list;
    }
}
